<?php
namespace  Admin\Controller;

use Utuo\BackgroundController;

class PartnerAdminController extends  BackgroundController
{  
	protected $pid;
	
	public function _initialize(){
		$this->setControllerTitle('管理员管理');
		$this->setActionTitle(array(
				'index' => '管理员列表',
				'add' => '添加管理员',
				'modify' => '修改管理员',
				'recycle' => '回收站',
				'setRole'=>'分配权限'
				
		),'','pid='.I('pid'));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
		$this->pid = I('pid', 0, "intval");
		$rbacPar = C('RBAC_PARTNER');
		$this->tableNames = \Utuo\Rbac::getAuthModelNames( array('role' => $rbacPar['RBAC_ROLE_TABLE'], 'user' => $rbacPar['RBAC_USER_TABLE'], 'access' => $rbacPar['RBAC_ACCESS_TABLE'], 'node' => $rbacPar['RBAC_NODE_TABLE']) );
	}
	
	// 登录跳转
	public function loginAction() {
		$id = I("id", 0, "intval"); // 管理员id
		$pid = $this->pid; // 合作商id
		$pid and $id = M("PartnerAdmin")->where('is_super=1 and pid=%d', $pid)->getField("id");
		$partner = M("partnerAdmin")->find($id);
		$_SESSION["_partner_"] = array(
				"aid" => $id,
				"id" => $partner['pid'],
				"name" => $partner['name']."(".session("name").")",
				self::LAST_ACTIVE => time(),
				self::IS_LOGINED => true,
		);
		$_SESSION["_partner_"]["sessionKeys"] = serialize(array_keys($_SESSION["_partner_"]));
		redirect(U("Partner/Index/index"), 0);
	}
	
	public function getLists($is_deleted) {
		$condition = array(
			'status'=>1,
			'is_deleted'=>$is_deleted,	
			'pid'=>$this->pid	
		);
		$partnerAdmin = M('PartnerAdmin')->where($condition)->select();
		$this->assign('partnerAdmin', $partnerAdmin)->display();
		
	}
	//是否开启角色分配
	public function isRoleAction($cid, $isrole) {
		M('PartnerAdmin')->where('id=%d',$cid)->setField("is_role",$isrole) ? $this->ajaxSuccess():$this->ajaxError();
	}
 	
 	//分配角色
	public function setRoleAction( $admin_id ) {
		$pid = M("PartnerAdmin")->where("id={$admin_id}")->getField("pid"); // 客户id
		$roles = M($this->tableNames['role'][1])->where("((pid={$pid} and aid=0) or (aid={$admin_id})) and is_deleted=%d", 0)->getField('id,name'); //合作商与客户的角色
		$curRoles = M($this->tableNames['user'][1])->where('admin_cid=%d',$admin_id)->getField("role_id",true); // 当前用户已分配的角色
		$data['roleCheckbox']=\Utuo\Form::makeCheckbox($roles, $curRoles, 'role_id[]');
		$data['adminCid'] = $admin_id;
		$this->assign("data",$data);
		$this->display();
	}
	
	//保存角色
	public function saveRoleAction(){
		$adminCid = I('admin_cid');
		$role_ids = I('role_id');
		D($this->tableNames['user'][1])->where('admin_cid=%d', $adminCid)->delete();
       if( is_array( $role_ids ) ) {
			foreach ($role_ids as $v) {
				D($this->tableNames['user'][1])->add(array(
				'admin_cid' => $adminCid,
				'role_id' => $v
				));
			}
			$this->ajaxSuccess();
		}
		$this->ajaxError();
	}
	
	public function addAction() {
		$partnerAdmin = array(
			'pid' => $this->pid,
		);
		$this->assign("partnerAdmin", $partnerAdmin);
	    $this->display("save");
	}
	
	public function modifyAction($id) {
		$condition = array(
				'status' =>1,
				'is_deleted' => 0,
				'pid' => $this->pid,
				'id' => $id
		);
		$partnerAdmin = M('PartnerAdmin')->where($condition)->find();
		$this->assign("partnerAdmin", $partnerAdmin);
		$this->display("save");
	}
	
	public function saveAction() {
		$this->submitPost( 'PartnerAdmin' );
	}
	
	// 删除数据
	public function removePostAction() {
		$this->ajaxRemove();
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete();
	}
	
	// 恢复管理员
	public function restorePostAction() {
		$this->ajaxRestore();
	}
	
	public function setStatusPostAction() {
		$this->ajaxSetStatus();
	}
	
}